﻿using System;
using System.Collections.Generic;  
using System.IO;    
namespace Problem02
{
    internal class Program
    {/// <summary>
     /// 将文件打开，计算所有数的平均值
     /// </summary>
     /// <param name="args"></param>
        static void Main(string[] args)
        {
            string filePath = @"D:\Data.txt";
            List<int> numbers = new List<int>();
            try
            {
                using (StreamReader reader = new StreamReader(filePath))
                {
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        if (int.TryParse(line, out int number))
                        {
                            numbers.Add(number);
                        }
                        else
                        {
                            Console.WriteLine("警告: 无法将以下行转换为数字: " + line);
                        }
                    }
                }
                if (numbers.Count > 0)
                {
                    double average = numbers.Average();
                    Console.WriteLine("文件中的数字的平均数是: " + average);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("读取文件时发生错误: " + ex.Message);
            }

        }
    }
}
